let cardWidth = $('.cards').width();
let gameRound = 1;
let canClick = true;

$('.g-container__hook').height(`${40 * gameRound}vw`);

let xw= $('.g-container').width() / 3;
let yw= $('.g-container').height() / gameRound;
// 1排
let runlist1 = [
    { "box": [{ "position": 1 }, { "position": 2 }]},
    { "box": [{ "position": 0 }, { "position": 2 }]},
    { "box": [{ "position": 0 }, { "position": 1 }]}
]
let boxmove1 = [
    { "move": [{ "moveX": 0 }, { "moveY": 0 }]},
    { "move": [{ "moveX": 1*xw }, { "moveY": 0 }]},
    { "move": [{ "moveX": 2*xw }, { "moveY": 0 }]}
]
// 2排
let runlist2 = [
    { "box": [{ "position": 1 }, { "position": 3 }] },
    { "box": [{ "position": 0 }, { "position": 2 }, { "position": 4 }]},
    { "box": [{ "position": 1 }, { "position": 5 }] },
    { "box": [{ "position": 0 }, { "position": 4 }] },
    { "box": [{ "position": 1 }, { "position": 3 }, { "position": 5 }]},
    { "box": [{ "position": 2 }, { "position": 4 }] }
]
let boxmove2 = [
    { "move": [{ "moveX": 0 }, { "moveY": 0 }]},
    { "move": [{ "moveX": 1*xw }, { "moveY": 0 }]},
    { "move": [{ "moveX": 2*xw }, { "moveY": 0 }]},
    { "move": [{ "moveX": 0 }, { "moveY": 1*yw }]},
    { "move": [{ "moveX": 1*xw }, { "moveY": 1*yw }]},
    { "move": [{ "moveX": 2*xw }, { "moveY": 1*yw }]}
]
// 3排
let runlist3 = [
    { "box": [{ "position": 1 }, { "position": 3 }, ] },
    { "box": [{ "position": 0 }, { "position": 2 }, { "position": 4 }] },
    { "box": [{ "position": 1 }, { "position": 5 }, ] },
    { "box": [{ "position": 0 }, { "position": 4 }, { "position": 6 }] },
    { "box": [{ "position": 1 }, { "position": 3 }, { "position": 5 }, { "position": 7 }] },
    { "box": [{ "position": 2 }, { "position": 4 }, { "position": 8 }] },
    { "box": [{ "position": 3 }, { "position": 7 }] },
    { "box": [{ "position": 4 }, { "position": 6 }, { "position": 8 }] },
    { "box": [{ "position": 5 }, { "position": 7 }] }
]
let boxmove3 = [
    { "move": [{ "moveX": 0 }, { "moveY": 0 }]},
    { "move": [{ "moveX": 1*xw }, { "moveY": 0 }]},
    { "move": [{ "moveX": 2*xw }, { "moveY": 0 }]},
    { "move": [{ "moveX": 0 }, { "moveY": 1*yw }]},
    { "move": [{ "moveX": 1*xw }, { "moveY": 1*yw }]},
    { "move": [{ "moveX": 2*xw }, { "moveY": 1*yw }]},
    { "move": [{ "moveX": 0 }, { "moveY": 2*yw }]},
    { "move": [{ "moveX": 1*xw }, { "moveY": 2*yw }]},
    { "move": [{ "moveX": 2*xw }, { "moveY": 2*yw }]}
]

let { runlistObj, boxmoveObj } = {
    runlistObj: {
        '3': runlist1,
        '6': runlist2,
        '9': runlist3
    },
    boxmoveObj: {
        '3': boxmove1,
        '6': boxmove2,
        '9': boxmove3
    }
}

// 重置宝箱容器大小
function resetCardsSize() {
    $('.g-container__hook').height(`${40 * gameRound}vw`);
    let cardsArr = $('.c-cards__card');
    for (let i = 0;i < cardsArr.length;i++ ) {
        let $card = $(cardsArr[i]);
        let cardNum = gameRound * 3;
        if (i < cardNum) {
            $card.show();
        } else {
            $card.hide();
        }
    }
    resetCardPos();
}
// 定位每个宝箱的位置
function resetCardPos() {
    yw= $('.g-container').height() / gameRound;
    let visibleCard = $(".c-cards__card:visible");
    let visibleCardL = $(".c-cards__card:visible").length;
    for (let i = 0;i < visibleCardL;i++ ) {
        let row = Math.floor(i / 3); // 横排
        let colum = i % 3; // 纵排
        $(visibleCard[i]).css({x:colum * xw,y: row * yw})
    }
}
resetCardsSize();

$('.game-star__hook').on('click', function() {
    if (canClick == false) {
        return;
    }
    canClick = false;
    let visibleCard = $(".c-cards__card:visible").length;
    let count = 0;
    let changeTime = 500;
    if (gameRound == 3) {
        changeTime = 100;
    }
    let timer = setInterval(function(){
        exchangePos(visibleCard,changeTime);
        if (gameRound == 3) { // 第三回合增加难度
            setTimeout(() => {
                exchangePos(visibleCard,changeTime);
            }, 100)
        }
        count++;
        changeTime -= (30 + gameRound*10);
        if (count >= gameRound*5) { // 下一回合
            canClick = true;
            clearInterval(timer)
            if (gameRound < 3) {
                gameRound += 1;
                resetCardsSize();
            }
        }
    }, 600 - gameRound*100)
})

function exchangePos(length,time) {
    time = time < 100 ? '100' : time;
    console.log(time)
    // 开始移动的盒子位置
    let startPos = Math.floor(Math.random() * length);
    // 可移动路径总数
    let routeCounts = runlistObj[length][startPos].box.length;
    // 交换的盒子位置
    let targetRandom = Math.floor(Math.random() * routeCounts);
    let targetPos = runlistObj[length][startPos].box[targetRandom].position;

    // console.log("开始card" + startPos + ",有" + routeCounts + "条路径,分别是");
    // for(let a of runlistObj[length][startPos].box) {
    //     console.log(a.position)
    // }
    console.log(`card${startPos}<->card${targetPos}`)
    // console.log("选择第" + (targetRandom-0+1) + "条" + "目标=" + targetPos);

    let boxStartTarget = boxmoveObj[length][startPos];
    let boxEndTarget = boxmoveObj[length][targetPos];

    // 和终点盒子位置交换
    let smove_top = boxEndTarget.move[1].moveY;
    let smove_left = boxEndTarget.move[0].moveX;
    // 和起始盒子位置交换
    let emove_top = boxStartTarget.move[1].moveY;
    let emove_left = boxStartTarget.move[0].moveX;
    // 改变对象中的位置
    boxStartTarget.move[0].moveX = smove_left;
    boxStartTarget.move[1].moveY = smove_top;
    boxEndTarget.move[0].moveX = emove_left;
    boxEndTarget.move[1].moveY = emove_top;

    $(".c-cards__card_"+startPos).animate({x:smove_left,y:smove_top},time);
    $(".c-cards__card_"+targetPos).animate({x:emove_left,y:emove_top},time);
}
